//
//  TBMainViewController.swift
//  新浪微博
//
//  Created by bin on 15/12/1.
//  Copyright © 2015年 XMG. All rights reserved.
//

import UIKit

class TBMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        tabBar.backgroundImage = UIImage(named: "tabbar_background")
//        addAllChildViewControllers()

    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(true)
        centerBtn.center.x = tabBar.bounds.width / 2.0
        centerBtn.center.y = tabBar.bounds.height / 2.0
//        centerBtn.frame = CGRectOffset(centerBtn.frame, tabBar.bounds.width / CGFloat(childViewControllers.count) * 2, 0)
        tabBar.addSubview(centerBtn)
    }
    
    lazy var centerBtn: UIButton = {

        let btn = UIButton(imageName: "tabbar_compose_icon_add", backgroundImage: "tabbar_compose_button")
        btn.addTarget(self, action: Selector("btnClick"), forControlEvents: .TouchUpInside)
        return btn
    }()
    
    @objc private func btnClick(){
        
        presentViewController(UIStoryboard(name: "Compose", bundle: nil).instantiateInitialViewController()!, animated: true, completion: nil)
    }
    
    func addAllChildViewControllers(){
        
        let path = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil)!
        let jsonData = NSData(contentsOfFile: path)!
        
        let profileArr = try? NSJSONSerialization.JSONObjectWithData(jsonData, options: .MutableContainers)
        guard let unWrappedArr = profileArr else{
            
            print("解析失败，手动添加控制器");
            addChildViewController("TBHomeViewController", andTitle: "首页", andImageName: "tabbar_home")
            addChildViewController("TBMessageViewController", andTitle: "消息", andImageName: "tabbar_message_center")
            addChildViewController("TBDiscoverViewController", andTitle: "发现", andImageName: "tabbar_discover")
            addChildViewController("TBProfileViewController", andTitle: "我", andImageName: "tabbar_profile")
            return
        }
        for item in unWrappedArr as! [[String: AnyObject]]{
//            print("\(item)")
            addChildViewController(item["vcName"] as! String, andTitle: item["title"] as! String, andImageName: item["imageName"] as! String)
        }
        
    }
    
    
    func addChildViewController(childController: String, andTitle title: String, andImageName imageName: String) {
        
        guard let nameSpace = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as? String else{
            print("fail")
            return
        }
        let cls = NSClassFromString(nameSpace+"."+childController) as! UITableViewController.Type
        let vc = cls.init()
        let navc = UINavigationController(rootViewController: vc)
        vc.title = title
        navc.tabBarItem.image = UIImage(named: imageName)
        navc.tabBarItem.selectedImage = UIImage(named: imageName+"_highlighted")
        addChildViewController(navc)
        print("\(vc)")
    }

}





